<?php
ifiperms_check('admin', 'stats'))
{
	//konfiguracja statystyk
	$days_pl=array('Niedziela', 'Poniedzialek', 'Wtorek', 'Sroda', 'Czwartek', 'Piatek', 'Sobota');
	$days_en=array('Sunday', 'Monday', 'Tuesday', 'Wendesday', 'Thursday', 'Friday', 'Saturday');

	$months_pl=array('Styczen','Luty','Marzec','Kwiecie�','Maj','Czerwiec','Lipiec','Sierpien','Wrzesien','Pazdziernik','Listopad','Grudzien');
	$months_en=array('January', 'February','March','April','May','June','July','August','September','October','November','December');
	//

	$db = new dbquery;
	$db2 = new dbquery;
	$db3 = new dbquery;

	if($_GET['cmd']=='online')
	{

		$fields=array('time', 'logged_in', 'ip', 'file');

		//sort
		if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
			$_GET['sort']='DESC';
		if(!$_GET['sortby'] or !in_array($_GET['sortby'], $fields))
			$_GET['sortby']='time';
		if($_GET['sort']=='ASC')
			$sortnext='DESC';
		else
			$sortnext='ASC';
		//

		$db->query("SELECT * FROM $conf[prefix]online ORDER by $_GET[sortby] $_GET[sort]") or $db->err(__FILE__, __LINE__);

		$content='
			<TABLE BORDER="0" WIDTH="100%" CLASS="tabelka2">
			<TR>
			<TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=online&sortby=ip&amp;sort='.$sortnext.'">IP:</A></TD>
			<TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=online&sortby=file&amp;sort='.$sortnext.'">'.$lang['admin_page'].'</A></TD>
			<TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=online&sortby=logged_in&amp;sort='.$sortnext.'">'.$lang['admin_users'].'</A></TD>
			</TR>';

		$y=0;
		while($d=$db->fetch_object())
		{
			$y++;
			$i++;
			if($i>2)
				$i=1;

			$exp=explode('/', $d->file);

			if($d->logged_in!=0) {
				$u=get_user_by_id($d->logged_in);
				$user='<a href="index.php?module=users&amp;user='.$d->logged_in.'" target="_blank">'.$u->login.'</a>';
			}
			else
				$user=$lang['admin_not_logged_in_user'];

			if(end($exp))
				$page=end($exp);
			else
				$page='index.php';

			$ctry='';
			$country='';

			$ext=explode(",", $d->http_accept_language);
			$ext=explode("-", $ext[0]);
			if(count($ext)>1) $ext=$ext[1]; else $ext=$ext[0];

			$country=countries('country', $ext);
			$ctry='<small>'.$country.'</small>';

			if($d->lang) 
				$lang_='<small>(<img src="admin/gfx/flags/f0-'.$d->lang.'.gif" />)</small>';

			$content.='
				<TR>
				<TD CLASS="row_'.$i.'" ALIGN="CENTER"><A HREF="http://www.ripe.net/perl/whois?&amp;searchtext='.$d->ip.'" TARGET="_blank">'.$d->ip.'</A> '.$lang_.'<br />'.$ctry.'</TD>
				<TD CLASS="row_'.$i.'" ALIGN="CENTER"><A HREF="'.$page.'" TARGET="_blank">'.substr($page, 0, 10).'...</A></TD>
				<TD CLASS="row_'.$i.'" ALIGN="CENTER">'.$user.'</TD>
				</TR>';
		}

		$content.='<TR>
			<TD CLASS="row_3" ALIGN="CENTER"><B>'.$lang['stats_sum'].' '.$y.'</B></TD>
			<TD COLSPAN="2"></TD>
			</TR>    
			</TABLE>'; 

		$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['admin_online']));
	}
	elseif($_GET['cmd']=='uni')
	{

		$fields=array('date', 'uni', 'raw');

		//sort
		if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
			$_GET['sort']='DESC';
		if(!$_GET['sortby'] or !in_array($_GET['sortby'], $fields))
			$_GET['sortby']='date';
		if($_GET['sort']=='ASC')
			$sortnext='DESC';
		else
			$sortnext='ASC';
		//  

		if(!$_GET['rec'])
			$_GET['rec']=0;

		if(!$_POST['from'] && $_GET['from'])
			$_POST['from']=$_GET['from'];

		if(!$_POST['to'] && $_GET['to'])
			$_POST['to']=$_GET['to'];

		if(!$_POST['from'])
			$value='yyyy-mm-dd';
		else
			$value=$_POST['from'];

		$content='
			<TABLE BORDER="0" WIDTh="100%">
			<TR>
			<FORM ACTION="'.$_SERVER['php_self'].'" METHOD="POST"><TD>'.$lang['msg_from'].' <INPUT TYPE="TEXT" NAME="from" CLASS="button" SIZE="11" onClick="javascript: if(this.value==\'yyyy-mm-dd\') this.value=\'\';" VALUE="'.$value.'"> '.$lang['msg_to'].' <INPUT TYPE="TEXT" NAME="to" VALUE="'.$_POST['to'].'" CLASS="button" SIZE="11"> <INPUT TYPE="SUBMIT" CLASS="button" VALUE="'.$lang['search'].'"></TD></FORM>
			<TD ALIGN="RIGHT"><A HREF="javascript:help_popup(\'index.php?module=stats&function=show_txt_stats&to='.$_POST['to'].'&from='.$_POST['from'].'&sort='.$_GET['sort'].'&amp;sortby='.$_GET['sortby'].'\');"  ALT="Export stats to txt - for Exel, ect." TITLE="Export stats to txt - for Exel, ect."><IMG SRC="admin/gfx/icons/icon_txt.gif" BORDER="0"></A>&nbsp;&amp;nbsp;<A HREF="javascript:help_popup(\'index.php?module=stats&function=show_standard_deviation&to='.$_POST['to'].'&from='.$_POST['from'].'&sort='.$_GET['sort'].'&sortby='.$_GET['sortby'].'\');" ALT="Standard deviation - days of the week" TITLE="Standard deviation - days of the week"><IMG SRC="admin/gfx/icons/icon_exe.gif" BORDER="0"></A></TD>
			</TR>
			</TABLE>
			<TABLE BORDER="0" WIDTH="100%" CLASS="tabelka2">
			<TR>
			<TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=uni&sortby=date&sort='.$sortnext.'&amp;limit='.$_GET['limit'].'">'.$lang['admin_date'].'</A></TD>
			<TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=uni&sortby=uni&sort='.$sortnext.'&amp;limit='.$_GET['limit'].'">UNI:</A></TD>
			<TD CLASS="row_3" ALIGN="CENTER"><A HREF="index.php?module=admin&action=stats&cmd=uni&sortby=raw&sort='.$sortnext.'&amp;limit='.$_GET['limit'].'">RAW:</A></TD>
			</TR>';

		//wy�wietl jako dni
		if(!$_GET['limit'])
		{
			if($_POST['from'] or $_POST['to']) {
				$fr=$_POST['from'];
				$to=$_POST['to'];

				if($_POST['from'] && $_POST['to'])
					$ft="WHERE `date` >= '$fr' && date <= '$to'";
				elseif($_POST['from'])
					$ft="WHERE `date` >= '$fr'";
				elseif($_POST['to'])
					$ft="WHERE `date` <= '$to'";
			}

			$db3->query("SELECT * FROM $conf[prefix]stats $ft") or $db->err(__FILE__, __LINE__);

			$uni_all=0;
			$raw_all=0;
			$all_all_uni=all_uni();
			$all_all_raw=all_raw();

			$db->query("SELECT * FROM $conf[prefix]stats $ft ORDER by $_GET[sortby] $_GET[sort] LIMIT $_GET[rec], $conf[admin_per_page]") or $db->err(__FILE__, __LINE__);
			while($d=$db->fetch_object())
			{
				$i++;
				if($i>2)
					$i=1;

				if($_SESSION['lang_short']=='pl')
					$day=$days_pl[date('w', strtotime($d->date))];
				else
					$day=$days_en[date('w', strtotime($d->date))];

				$content.='
					<TR>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><I>'.$day.'</I> <small>'.date('d-m-Y', strtotime($d->date)).'</small></TD>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><B>'.$d->uni.'</B></TD>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><B>'.$d->raw.'</B></TD>
					</TR>';
				$uni_all+=$d->uni;
				$raw_all+=$d->raw;
			}

			//dzielenie na strony 
			$page_link=split_to_pages('<A HREF="index.php?module=admin&action=stats&cmd=uni&rec={rec}&from='.$_POST['from'].'&to='.$_POST['to'].'&sort='.$_GET['sort'].'&amp;sortby='.$_GET['sortby'].'">{nr}</A>', $conf['admin_per_page'], $db3->num_rows(), $_GET['rec']);
			//	  
		}
		//koniec wy�wietl jako dni
		//wy�wietl jako miesi�ce
		elseif($_GET['limit']=='months')
		{
			if($_POST['from'] or $_POST['to']) {
				$fr=$_POST['from'];
				$to=$_POST['to'];

				if(strlen($fr)==7)
					$fr.='-01';
				if(strlen($to)==7)
					$to.='-01';			

				if($_POST['from'] && $_POST['to'])
					$ft="WHERE `date` >= '$fr' && date <= '$to'";
				elseif($_POST['from'])
					$ft="WHERE `date` >= '$fr'";
				elseif($_POST['to'])
					$ft="WHERE `date` <= '$to'";
			}


			$db->query("SELECT * FROM $conf[prefix]stats $ft ORDER by date ASC LIMIT 0,1") or $db->err(__FILE__, __LINE__);

			$date_min=$db->fetch_object();
			$date_min=$date_min->date;
			$date_min=date('Y-m', strtotime($date_min))."-01 00:00";

			$db->query("SELECT * FROM $conf[prefix]stats $ft ORDER by date DESC LIMIT 0,1") or $db->err(__FILE__, __LINE__);

			$date_max=$db->fetch_object();
			$date_max=$date_max->date;
			$date_max=date('Y-m', strtotime($date_max))."-01 00:00";  

			$y=0;

			while($date_min<=$date_max)
			{
				$i++;
				if($i>2)
					$i=1;

				$date=substr($date_min, 0, 7);

				$db2->query("SELECT * FROM $conf[prefix]stats WHERE date LIKE '$date%'") or $db->err(__FILE__, __LINE__);

				$uni=0;
				$raw=0;

				while($d=$db2->fetch_object())
				{
					$uni+=$d->uni;
					$raw+=$d->raw;
				}

				$array[$y]=array('date'=>$date_min, 'uni'=>$uni, 'raw'=>$raw);
				$y++;
				//dodajemy jeden miesi�c 
				$date_min=date('Y-m', strtotime("+1 month", strtotime($date_min)))."-01 00:00"; 	    
			}

			//sortowanie
			$array=stats_sort($array, $_GET['sortby'], $_GET['sort']);
			//

			$uni_all=all_uni();
			$raw_all=all_raw();

			for($x=0;$x<$y;$x++)
			{
				if($_SESSION['lang_short']=='pl')
					$month=$months_pl[date('n', strtotime($array[$x]['date']))-1];
				else
					$month=$months_en[date('n', strtotime($array[$x]['date']))-1];	 

				$content.='
					<TR>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><I>'.$month.'</I> <small>'.date('m-Y', strtotime($array[$x]['date'])).'</small></TD>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><B>'.$array[$x]['uni'].'</B> / '.$uni_all.' - <I>'.(percent($array[$x]['uni'], $uni_all)).'</I></TD>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><B>'.$array[$x]['raw'].'</B> / '.$raw_all.' - <I>'.(percent($array[$x]['raw'], $raw_all)).'</I></TD>
					</TR>
					<TR>
					<TD></TD>
					<TD><IMG SRC="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['stats_graph'].'" BORDER="0" HEIGHT="10" WIDTH="'.((@round(($array[$x]['uni'] / $uni_all)*100, 0))).'%"></TD>
					<TD><IMG SRC="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['stats_graph'].'" BORDER="0" HEIGHT="10" WIDTH="'.((@round(($array[$x]['raw'] / $raw_all)*100, 0))).'%"></TD>
					</TR>';	  
			} 	  
		}
		//koniec wy�wietl jako miesi�ce
		//wy�wietl jako lata
		elseif($_GET['limit']=='years')
		{
			$db->query("SELECT * FROM $conf[prefix]stats ORDER by date ASC LIMIT 0,1") or $db->err(__FILE__, __LINE__);
			$date_min=$db->fetch_object();
			$date_min=date('Y', strtotime($date_min->date));

			$db->query("SELECT * FROM $conf[prefix]stats ORDER by date DESC LIMIT 0,1") or $db->err(__FILE__, __LINE__);
			$date_max=$db->fetch_object();
			$date_max=date('Y', strtotime($date_max->date));  

			$y=0;
			while($date_min<=$date_max)
			{
				$i++;
				if($i>2)
					$i=1;

				$uni=0;
				$raw=0;

				$db2->query("SELECT * FROM $conf[prefix]stats WHERE date LIKE '$date_min%'") or $db->err(__FILE__, __LINE__);
				while($d=$db2->fetch_object())
				{
					$uni+=$d->uni;
					$raw+=$d->raw;
				}

				$array[$y]=array('date'=>$date_min, 'uni'=>$uni, 'raw'=>$raw);
				$y++;
				$date_min++;
			}

			//sortowanie
			$array=stats_sort($array, $_GET['sortby'], $_GET['sort']);
			//

			$uni_all=all_uni();
			$raw_all=all_raw();

			for($x=0;$x<$y;$x++)
			{
				$content.='
					<TR>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><I>'.$array[$x]['date'].'</I></TD>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><B>'.$array[$x]['uni'].'</B> / '.$uni_all.' - <I>'.(percent($array[$x]['uni'], $uni_all)).'</I></TD>
					<TD CLASS="row_'.$i.'" ALIGN="CENTER"><B>'.$array[$x]['raw'].'</B> / '.$raw_all.' - <I>'.(percent($array[$x]['raw'], $raw_all)).'</I></TD>
					</TR>
					<TR>
					<TD></TD>
					<TD><IMG SRC="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['stats_graph'].'" BORDER="0" HEIGHT="10" WIDTH="'.((@round(($array[$x]['uni'] / $uni_all)*100, 0))).'%"></TD>
					<TD><IMG SRC="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['stats_graph'].'" BORDER="0" HEIGHT="10" WIDTH="'.((@round(($array[$x]['raw'] / $raw_all)*100, 0))).'%"></TD>
					</TR>';	  
			} 	  
		}
		//koniec wy�wietl jako lata

		$content.='
			<TR>
			<TD CLASS="row_3" ALIGN="CENTER"><B>UNI '.$lang['stats_sum'].' '.$uni_all.'</B></TD>
			<TD COLSPAN="2" ROWSPAN="2" ALIGN="CENTER">'.$lang['sort_by'].' <A HREF="index.php?module=admin&action=stats&amp;cmd=uni">'.$lang['stats_sort_days'].'</A> <A HREF="index.php?module=admin&action=stats&cmd=uni&amp;limit=months">'.$lang['stats_sort_months'].'</A> <A HREF="index.php?module=admin&action=stats&cmd=uni&amp;limit=years">'.$lang['stats_sort_years'].'</A></TD>
			</TR>
			<TR>
			<TD CLASS="row_3" ALIGN="CENTER"><B>RAW '.$lang['stats_sum'].' '.$raw_all.'</B></TD>
			</TR>
			</TABLE>
			<TABLE BORDER="0" WIDTH="100%">
			<TR>
			<TD COLSPAN="2">'.$page_link.'</TD>
			</TR>
			<TR>
			<TD WIDTH="50"></TD>
			<TD COLSPAN="3">'.$lang['stats_hint1'].'<BR>'.$lang['stats_hint2'].'</TD>
			</TR>
			</TABLE>';

		$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['stats_uni']));
	}
}
//
?>
